
import type {DefaultTheme} from 'vitepress/types/default-theme.d.ts';

import * as fs from 'node:fs'
import * as path from "path";


export function GenSidebar(base: string, dir: string): DefaultTheme.SidebarItem[] {
    const result :DefaultTheme.SidebarItem[] = []
    const files = fs.readdirSync(path.join(base, dir));
    for (const file of files) {
        const fp = path.join(dir, file);
        const name = file.split(".")[0]
        // 递归处理目录
        if (fs.statSync(path.join(base, fp)).isDirectory()) {
            const items = GenSidebar(base, fp);
            if (items.length !== 0) {
                result.push({text: name, items: items})
            } else {
                result.push({text: name, collapsed: false, link: GetPathName(fp)})
            }
            continue
        }
        // 处理默认文件
        if (name === "index") {
            continue
        }
        // 处理其它文件
        result.push({text: name,collapsed: false,  link: `${GetPathName(dir)}${name}`})
    }


    return result
}

function GetPathName(dir: string) : string {
    const pathNames:string[] = []
    let currentPath = path.normalize(dir);
    while (currentPath !== '.' && currentPath !== '/') {
        const name = path.basename(currentPath);
        pathNames.unshift(name);
        currentPath = path.dirname(currentPath);
    }
    return `/${pathNames.join("/")}/`
}


//
//
//
// function generateSidebar(items: FolderInfo[], sidebar={}): DefaultTheme.Sidebar {
//     items.forEach(item => {
//         const { items: subItems, isSidebar, path} = item
//         if (isSidebar) {
//             sidebar[`/${path}/`] = [item]
//         } else if (subItems) {
//             generateSidebar(subItems, sidebar)
//         }
//     })
//     return sidebar
// }
//
// async function getFolderInfo(root: string, absoluteDir: string): Promise<FolderInfo> {
//     const foldernameWithoutExt = getNameWithoutExt(absoluteDir)
//     const items: FolderInfo[] = []
//     const isSidebar = foldernameWithoutExt.endsWith('-sidebar')
//     const relativeDir = path.relative(root, absoluteDir).replace(/\\/g, '/');
//     const folderInfo: FolderInfo = {
//         text: foldernameWithoutExt.replace(/-sidebar$/, ''),
//         isSidebar,
//         path: relativeDir,
//         collapsed: true,
//         items,
//     }
//
//     const files = await fs.readdir(absoluteDir);
//
//     for (const filename of files) {
//         const filePath = path.join(absoluteDir, filename);
//         const stat = await fs.stat(filePath);
//         const fileNameWithoutExt = getNameWithoutExt(filename)
//
//         // 如果是隐藏文件，则跳过当前循环
//         if (fileNameWithoutExt.endsWith('-hide')) {
//             continue
//         }
//         // 如果是目录，则递归
//         if (stat.isDirectory()) {
//             items.push(await getFolderInfo(root, filePath))
//             continue
//         }
//         // 如果后缀名是.md（忽略大小写），则需要添加link属性
//         if (path.extname(filename).toLowerCase() === '.md') {
//             // 如果是index.md，则向folderInfo上添加link属性，否则向folderInfo.items添加
//             if (fileNameWithoutExt.toLowerCase() === 'index') {
//                 folderInfo.link = `/${relativeDir}/index.html`
//             } else {
//                 items.push({
//                     text: fileNameWithoutExt,
//                     link: `/${relativeDir}/${fileNameWithoutExt}.html`
//                 })
//             }
//         }
//     }
//
//     return folderInfo
// }
//
// function getNameWithoutExt(filePath: string) {
//     const fileName = path.basename(filePath);
//     const fileNameWithoutExt = path.parse(fileName).name;
//     return fileNameWithoutExt;
// }
//
//
// export default function autoGenerateSidebar(options: AutoSidebarOption): Plugin {
//     console.log(options);
//     const { docRoot } = options
//     const root = path.resolve(__dirname, docRoot)
//
//     return {
//         name: 'vite-plugin-vitepress-autoSidebar',
//         configureServer ({
//                              watcher,
//                              restart
//                          }: ViteDevServer) {
//             const fsWatcher = watcher.add('*.md');
//             fsWatcher.on('all', async (event, path) => {
//                 if (event !== 'change') {
//                     console.log(`${event} ${path}`);
//                     try {
//                         await restart();
//                         // ws.send({
//                         //   type: 'full-reload',
//                         // })
//                         console.log('update sidebar...');
//                     } catch {
//                         console.log(`${event} ${path}`);
//                         console.log('update sidebar failed');
//                     }
//                 }
//             });
//         },
//         async config(config) {
//             const folderInfo = await getFolderInfo(root, root);
//             (config as UserConfig).vitepress.site.themeConfig.sidebar = folderInfo.items ? generateSidebar(folderInfo.items) : {}
//             return config
//         }
//     }
// }